<!--
 * @Descripttion: 系统计划任务配置
 * @version: 1.2
 * @Author: sakuya
 * @Date: 2021年7月7日09:28:32
 * @LastEditors: sakuya
 * @LastEditTime: 2021年7月10日20:56:47
-->

<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="450"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			:model="form"
			:rules="rules"
			ref="dialogForm"
			label-width="80px"
			label-position="left"
		>
			<el-form-item label="名称" prop="name">
				<el-input
					v-model="form.name"
					placeholder="名称"
					clearable
				></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
</template>

<script>
import scCron from "@/components/scCron";

export default {
	name: "AIApplicationManageSave",
	components: {
		scCron,
	},
	emits: ["success", "closed"],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增应用",
				edit: "编辑应用",
			},
			form: {
				name: "",
			},
			rules: {
				name: [
					{
						required: true,
						message: "请输入名称",
						trigger: "blur",
					},
				],
			},
			visible: false,
			isSaveing: false,
		};
	},
	async mounted() {},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.form.id == null || this.form.id == "") {
						res = await this.$API.aiApplication.create.post(
							this.form
						);
					} else {
						res = await this.$API.aiApplication.update.post(
							this.form
						);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("success");
						this.visible = false;
						this.$message.success("操作成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			let deepCopiedObject = JSON.parse(JSON.stringify(data));
			this.form = deepCopiedObject;
		},
	},
};
</script>

<style></style>
